package com.woniuxy.education.controller;

import com.woniuxy.education.entity.*;
import com.woniuxy.education.service.StudentService;
import com.woniuxy.education.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController extends ExceptionCenter{
    @Autowired
    StudentService studentService;
    //通过id查询该学生下面的作业
    @RequestMapping("/homework")
    public PageResult<List<Homework>> homework(@RequestBody SelectWork selectWork){
        return studentService.findHomework(selectWork) ;

    }

    //通过homeworkid查询具体题目
    @RequestMapping("/findAllTitle")
    public AllTitle findAllTitle(int id){
        return studentService.findAllTitle(id);
    }

    //接收前端传过来的已完成提交的作业或者考试
    @RequestMapping("/send")
    public ResponseResult send(@RequestBody ALLanswer allanswer){
        ResponseResult responseResult =new ResponseResult();
        //向数据库插入提交的答案
        //若成功,修改当前作业状态
       if(studentService.insertCompleted_work(allanswer)){
           //如果是家庭作业
           if(allanswer.getState()==1){
               if(studentService.updateHomeworkstatus(allanswer.getHomeortestid())) {
                   responseResult.setCode(200);
                   responseResult.setMessage("已提交");
                   return responseResult;
               }
           }
           //如果是考试
           if(allanswer.getState()==2){
               if(studentService.updateTeststatus(allanswer.getHomeortestid())) {
                   responseResult.setCode(200);
                   responseResult.setMessage("已提交");
                   return responseResult;
               }
           }

       }
       responseResult.setCode(400);
       responseResult.setMessage("提交失败,请稍后重试");
        return responseResult;
    }


    //通过id查询该学生下面的考试条目
    @RequestMapping("/testPage")
    public PageResult<List<TestPage>> testPage(int c_id, int page, int size){
        return studentService.findtestPage(c_id,page,size) ;
    }

    //通过testid查询具体题目
    @RequestMapping("/findtestTitle")
    public AllTitle findtestTitle(int id){
        return studentService.findtestTitle(id);
    }

    //学生提问
    @RequestMapping("/insertquestion")
    public ResponseResult insertquestion(@RequestBody Question_ask question_ask){
        System.out.println(question_ask);
        ResponseResult responseResult =new ResponseResult();
        if(studentService.insertquestion(question_ask)){
            responseResult.setCode(200);
            responseResult.setMessage("提交成功");
            return responseResult;
        }else {
            responseResult.setCode(400);
            responseResult.setMessage("提交失败,请稍后再试");
            return responseResult;
        }
    }

    //查找所有学生提的问题提问
    @RequestMapping("/findAllQuestion")
    public PageResult<List<Question_ask>>  findAllQuestion(int page, int size){
      return studentService.findAllQuestion(page, size);
    }

    //查找所有学生考试数据
    @RequestMapping("/findTestData")
    public ListTestScore  findTestData(int level,int userid,int c_id){
        return studentService.findTestData(level,userid,c_id);
    }

    //查询已做的题库
    @RequestMapping("/findquestionisdo")
    public List<Allquestions> findquestionisdo(int u_id){
        return studentService.findquestionisdo(u_id) ;
    }

    //学生点击收藏题目
    @RequestMapping("/collectiontitle")
    public boolean collectiontitle(int titleid,int u_id,int titleType){
        return studentService.collectiontitle(titleid,u_id,titleType);
    }

    //学生收藏的题目
    @RequestMapping("/findCollectTitle")
    public List<Allquestions> findCollectTitle(int u_id){
        return studentService.findCollectTitle(u_id);
    }

    //学生点击取消收藏题目
    @RequestMapping("/cancelcollect")
    public boolean cancelcollect(int titleid,int u_id,int titleType){
        return studentService.cancelcollect(titleid,u_id,titleType);
    }

    //查询所有通过的文章
    @RequestMapping("/findPassArticle")
    public PageResult<List<Article>> findPassArticle(int page,int size){
        return studentService.findPassArticle(page,size);
    }

    //开始刷题endOfDoTitle
    @RequestMapping("/brushTopic")
    public AllTitle  brushTopic(@RequestBody AllClassify allClassify){
        System.out.println(allClassify);
        return studentService.brushTopic(allClassify);
    }

    //结束刷题
    @RequestMapping("/endOfDoTitle")
    public boolean  endOfDoTitle(@RequestBody AllClassify allClassify){
       return studentService.endOfDoTitle(allClassify);
    }

    //@RequiresRoles(value = {"superadmin","admin"},logical = Logical.OR) //必须是admin角色才能访问,ligical可以设置或者和并且
//    @RequiresPermissions(value = {"teacher:del"})//上面是通过角色,这个是通过权限
//    @RequestMapping("/del")
//    public String del(){
//        System.out.println("删除teacher");
//        return "success";
//    }
}
